import { editProduct } from '@Api/sysApi/productApi'

export function format(data) {
    // console.log('提交的数据', data)
    // const upDataAlls = setItemInfoData(data[0])
    return new Promise((resolve, reject) => {
        editProduct(setItemInfoData(data)).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
    // const gterInters = data.map(item => {
    //     return editProduct(setItemInfoData(item))
    // })
    // return Promise.all(gterInters).then(res => {
    //     console.log('提交成功', res)
    // })
    // console.log('POST请求发送的数据', upDataAlls)
    // editProduct(upDataAlls).then(res => {
    //     console.log('提交成功', res)
    // })
}

export function setItemInfoData(data) {
    const { setReForData } = data
    // console.log('提交的数据', setReForData)
    const { deleailReFodata, picimageList, prettify_info } = setReForData

    // console.log('商品详情', deleailReFodata)
    // console.log('主图', picimageList)
    // console.log('详情图', prettify_info)
    const {
        product_id,
        product_type,
        category_detail,
        name,
        recommend_remark,
        pay_type,
        delivery_method,
        cdf_category,
        reduce_type,
        freight_id,
        weight_value,
        weight_unit,
        delivery_delay_day,
        presell_type,
        presell_delay,
        presell_end_time,
        mobile,
        out_product_id,
        quality_list,
        spec_pics,
        maximum_per_order,
        limit_per_buyer,
        minimum_per_order,
        product_format_new,
        spu_id,
        appoint_delivery_day,
        extra,
        outer_product_id,
        standard_brand_id,
        poi_resource = null,
        car_vin_code,
        presell_config_level,
        need_recharge_mode,
        account_template_id,
        presell_delivery_type,
        white_back_ground_pic_url,
        long_pic_url,
        after_sale_service,
        sell_channel,
        start_sale_type,
        delay_rule,
        material_video_id,
        pickup_method,
        size_info_template_id,
        sale_channel_type,
        name_prefix,
        store_id,
        main_product_id,
        sale_limit_id,
        reference_price,
        reference_price_certificate,
        main_image_three_to_four,
        main_pic_3_4,
        unit_price_info = null,
        quality_inspection_info = null,
        is_c2b_switch_on = false,
        micro_app_id,
        is_auto_charge = false,
        short_product_name,
        after_sale_service_v2,
        spec_prices,
        name_suffix,
        use_brand_name = false
    } = deleailReFodata

    const { fourth_cid, third_cid, second_cid, first_cid } = category_detail
    const category_leaf_id = fourth_cid || third_cid || second_cid || first_cid // 类目
    const pic = picimageList.map(item => item.url).join("|") // 主图
    const description = prettify_info.map(item => item.url).join("|") // 详情图
    const spec_pic_str = spec_pics.map(item => {
        // [].includes
        if (!item.pic.includes('https://p3-aio.ecombdimg.com/obj/')) {
            return 'https://p3-aio.ecombdimg.com/obj/' + item.pic
        }
        return item.pic
    }).join("|") // 规格图
    const main_image_three_to_four_str = main_pic_3_4.map(item => item).join("|") // 3:4主图
    // console.log('spec_prices', spec_prices)
    const spec_prices_v2 = formatSpecPic(spec_prices)
    const dataInfo = {
        product_id,
        product_type,
        category_leaf_id,
        name,
        recommend_remark,
        pic,
        description,
        // pay_type, // 已经废弃
        delivery_method,
        cdf_category,
        reduce_type,
        freight_id,
        weight: weight_value,
        weight_unit, // weight重量。数据范围需要小于10000，如果超过，请更换的weight_unit weight_value 有放回值

        delivery_delay_day,
        presell_type,
        presell_delay,
        presell_end_time,
        // supply_7day_return【该字段将在2023年4月30日下线，请开发使用最新的after_sale_service字段传值；】是否支持7天无理由，0不支持，1支持，2支持（拆封后不支持）
        mobile,
        commit: true, // 保存提交 并发布
        remark: '抖羚羊批量编辑', // 备注信息
        out_product_id,
        quality_list, // 质量承诺
        // spec_name, // 废弃
        // specs, //废弃
        // spec_prices: [], // 废弃
        spec_pic: spec_pic_str,
        maximum_per_order,
        limit_per_buyer,
        minimum_per_order,
        product_format_new: (() => {
            // console.log('product_format_new', product_format_new)
            const StrToJson = JSON.parse(product_format_new)
            Object.keys(StrToJson).forEach((key) => {
                StrToJson[key].forEach((item) => {
                    // item.Name 是否为字母开头
                    if ((item.PropertyName == '型号' || item.PropertyName == '货号') && !/^[a-zA-Z]/.test(item.Name)) {
                        item.Name = 'd' + item.Name
                        if (item.Name.length < 2) {
                            item.Name = item.Name + 'ly'
                        }
                    }
                })
            })
            return JSON.stringify(StrToJson)
        })(),
        spu_id,
        appoint_delivery_day,
        // third_url ???
        extra,
        // src
        outer_product_id,
        standard_brand_id,
        // need_check_out ??? 卡券需要true
        // force_use_quality_list: true // 强制使用质量承诺 是否使用店铺的资质
        car_vin_code,
        presell_config_level,
        need_recharge_mode,
        account_template_id,
        presell_delivery_type,
        white_back_ground_pic_url, // 白底图
        long_pic_url,
        // after_sale_service222: after_sale_service,
        after_sale_service: (() => {
            let dater = {
                supply_day_return_selector: ""
            }
            try {
                const getDater = JSON.parse(after_sale_service)
                dater.supply_day_return_selector =
                    getDater.supply_day_return_selector
            } catch (error) {
                console.log("售后字段", error)
            }
            return JSON.stringify(dater)
        })(),
        sell_channel,
        start_sale_type,
        delay_rule,
        material_video_id,
        pickup_method,
        size_info_template_id,
        // substitute_goods_url 外部商品ID
        sale_channel_type,
        name_prefix,
        store_id,
        main_product_id,
        sale_limit_id,
        reference_price,
        reference_price_certificate,
        main_image_three_to_four: main_image_three_to_four_str,
        unit_price_info, // 
        quality_inspection_info, //
        is_c2b_switch_on, //
        micro_app_id,
        is_auto_charge,
        short_product_name,
        after_sale_service_v2: (() => {
            const adder = {
                is_large_product: after_sale_service_v2.is_large_product
            }
            if (after_sale_service_v2.three_guarantees) {
                adder.three_guarantees = {
                    service_type: after_sale_service_v2.three_guarantees.service_type,
                    duration:
                        after_sale_service_v2.three_guarantees.service_type == 1 &&
                            after_sale_service_v2.three_guarantees.duration === 180
                            ? 365
                            : after_sale_service_v2.three_guarantees.duration
                }
            }
            return adder
        })(),
        spec_prices_v2, // 规格信息
        // spec_info // 规格模版
        with_sku_type: false,
        name_suffix,
        use_brand_name, // 是否使用品牌名
        // open_logistics_info,
        // custom_property
    }
    if (poi_resource) {
        dataInfo.need_check_out = true
        dataInfo.poi_resource = poi_resource
    }
    // console.log('提交的数据', dataInfo)

    return dataInfo
}

function formatSpecPic(spec_prices) {
    return spec_prices.map(item => {
        const {
            multi_time_stocks = null,
            sku_id,
            out_sku_id,
            sku_status,
            sku_classification_type,
            sell_properties,
            cargo_info = null,
            gold_process_charge,
            spec_detail_ids,
            stock_num,
            price,
            code,
            step_stock_num,
            prom_stock_num,
            prom_step_stock_num,
            sku_type,
            supplier_id,
            promotion_stock_num,
            promotion_step_stock_num,
            customs_report_info,
            lock_stock_num,
            lock_step_stock_num,
            stock_num_map,
            tax_exemption_sku_info,
            presell_delay,
            delivery_infos
        } = item
        const refoDatas = {
            // multi_time_stocks: [],
            sku_status,
            sku_classification_type,
            sell_properties,
            // cargo_info: {
            //     source_type: 0,
            //     cargo_id: ''
            // },
            gold_process_charge,
            sku_id,
            out_sku_id,
            outer_sku_id: '',
            spec_detail_ids,
            stock_num,
            price,
            code,
            step_stock_num,
            prom_stock_num,
            prom_step_stock_num,
            sku_type,
            supplier_id,
            promotion_stock_num,
            promotion_step_stock_num,
            customs_report_info,
            lock_stock_num,
            lock_step_stock_num,
            stock_num_map,
            tax_exemption_sku_info,
            presell_delay,
            delivery_infos
        }
        if (multi_time_stocks) {
            refoDatas.multi_time_stocks = multi_time_stocks
        }
        if (cargo_info) {
            refoDatas.cargo_info = cargo_info
        }

        return refoDatas
    })
}